import datetime
import uuid

import yaml
from fastapi.requests import Request


def get_current_time(f: str = "%Y-%m-%d %H:%M:%S") -> str:
    """
    获取当前时间
    :param f: 时间格式
    :return: 字符串
    """
    now = datetime.datetime.now()
    return now.strftime(f)


def load_yaml(file_path: str) -> dict:
    """
    加载yaml文件
    :param file_path: 文件路径
    :return: 字典
    """
    with open(file_path, 'r', encoding="utf-8") as f:
        data = yaml.safe_load(f)
    return data


def save_yaml(file_path: str, data: dict) -> bool:
    """
    保存yaml文件
    :param file_path: 文件路径
    :param data: 数据
    :return: bool
    """
    with open(file_path, "w", encoding="utf-8") as f:
        yaml.dump(data, f, allow_unicode=True, default_flow_style=False)

    return True


def generate_uuid() -> str:
    """
    生成uuid
    :return: uuid str
    """
    return uuid.uuid4().hex


def get_session_id(request: Request):
    if "session_id" not in request.session:
        request.session["session_id"] = generate_uuid()

    return request.session["session_id"]
